import { post, get, put, del } from '@/utils/request';
import { Function, FunctionFormValues } from '@/pages/backhub/function/store/functionTypes';

/**
 * 获取函数列表
 * @returns 函数列表
 */
export const getFunctionList =  async (params?: {
    page?: number;
    pageSize?: number;
    keyword?: string;
    type?: string;
    status?: string;
  }): Promise<any> => {
  return get(`/engine/starhub/function/page`, { params });
};

/**
 * 获取函数
 * @returns 函数信息
 */
export const getFunctionDetail = async (id: string): Promise<Function[]> => {
  return get(`/engine/starhub/function/${id}`);
};

/**
 * 创建函数
 * @param values 函数表单值
 * @returns 创建的函数
 */
export const createFunction = async (values: FunctionFormValues): Promise<Function> => {
  return post(`/engine/starhub/function/add`, values);
};

/**
 * 更新函数
 * @param id 函数ID
 * @param values 函数表单值
 * @returns 更新的函数
 */
export const updateFunction = async (id: string, values: FunctionFormValues): Promise<Function> => {
  values.id=id;
  return post(`/engine/starhub/function/update`, values);
};


/**
 * 删除函数
 * @param id 函数ID
 */
export const deleteFunction = async (id: string): Promise<void> => {
  return post(`/engine/starhub/function/delete/${id}`);
};


// 批量删除函数
export const batchDeleteFunctions = async (ids: string[]): Promise<void> => {
  return post(`/engine/starhub/function/batchDelete`, { data: { ids } });
};

// 更新函数状态
export const updateFunctionStatus = async (id: string, status: string): Promise<void> => {
  return post(`/engine/starhub/function/enable/${id}`, { status });
};

// 批量更新函数状态
export const batchUpdateFunctionStatus = async (ids: string[], status: string): Promise<void> => {
  return post(`/engine/starhub/function/enable/batchUpdate`, { ids, status });
};

// 验证函数标识是否可用
export const checkFunctionIdentifier = async (functionName: string): Promise<boolean> => {
  return post(`/engine/starhub/function/${functionName}`);
};

// 获取函数调用统计
export const getFunctionStatistics = async (id: string, params?: {
  startTime?: string;
  endTime?: string;
}): Promise<{
  totalCalls: number;
  successCalls: number;
  failedCalls: number;
  averageResponseTime: number;
}> => {
  return get(`/engine/starhub/functions/${id}/statistics`, { params });
};